﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Security.Cryptography;
using System.Text;

namespace OA.Controllers
{
    public class ChangeCardPwdController : Controller
    {
        //
        // GET: /ChangeCardPwd/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult changeCardPwd()
        {
            string memberId = System.Web.HttpContext.Current.Request.QueryString["memberId"];
            memberInfo MI = new memberInfo();
            memberInfoManager MIManager = new memberInfoManager();
            if (!string.IsNullOrEmpty(memberId))
            {
                MI = MIManager.GetModel(memberId);
                ViewData["memberId"] = MI.memberId;
                ViewData["memberName"] = MI.memberName;
            }
            if (Session["memberId"] != null)
            {
                MI = MIManager.GetModel(Session["memberId"].ToString());
                ViewData["memberId"] = MI.memberId;
                ViewData["memberName"] = MI.memberName;
            }
            
            return View();
        }
        
        [HttpPost]
        public ActionResult changeCardPwd( FormCollection fc)
        {
            string memberId = fc["memberId"];
            string memberName = fc["memberName"];
            string oldPwd = fc["oldPwd"];
            string newPwd = fc["newPwd"];
            string comfirm_Pwd = fc["comfirm_Pwd"];
            memberInfo MI = new memberInfo();
            memberInfoManager MIManager = new memberInfoManager();
            MI = MIManager.GetModel(memberId);
          
            if (MI == null)
            {
                return Content("<script>alert('没有找到该会员')</script>");
            }
            if (MI.pwd == null || MI.pwd == "未设置")
            {
                if (!string.IsNullOrEmpty(oldPwd))
                {
                    return Content("<script>alert('你以前没有设置过密码,请勿填写旧密码'); window.location.href='/changeCardPwd/changeCardPwd/?memberId="+memberId+"'</script>");
                }

            }
            else
            {
                if (MI.pwd != Md5(oldPwd))
                {
                    return Content("<script>alert('旧密码输入有误');window.location.href='/changeCardPwd/changeCardPwd/?memberId=" + memberId + "'</script>");
                }
            }
           
            if (string.IsNullOrEmpty(newPwd))
            {
                return Content("<script>alert('新密码不能为空'); window.location.href='/changeCardPwd/changeCardPwd/?memberId=" + memberId + "'</script>");
            }
            if (comfirm_Pwd != newPwd)
            {
                return Content("<script>alert('两次输入的密码不一致'); window.location.href='/changeCardPwd/changeCardPwd/?memberId=" + memberId + "'</script>");
            }


            MI.pwd = Md5(newPwd);
            MIManager.Update();
            return Content("<script>alert('修改成功');parent.layer.close(parent.layer.getFrameIndex())</script>");
        }
        public string Md5(string str)
        {
            MD5 md = new MD5CryptoServiceProvider();
            byte[] btt = Encoding.Default.GetBytes(str);
            btt = md.ComputeHash(btt);
            string newStr = BitConverter.ToString(btt).Replace("-", "");
            return newStr;
        }
    }
}
